package org.setamv.springcloud.feignclient;

import org.setamv.springcloud.entity.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 定义访问User服务的Feign客户端
 * <p>
 * 指定Hystrix回退类为 {@link UserServiceFallbackClient}
 */
@FeignClient(value = "service", fallback = UserServiceFallbackClient.class)
public interface UserServiceClient {

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE)
    User getUser(@PathVariable("id") String id);

    /**
     * 访问一个超时的服务
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/{id}/slowly", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE)
    User getUserSlowly(@PathVariable("id") String id);

    /**
     * 注意：这里的参数ids不能使用集合，因为集合在转换为url的参数时会变成这种格式：[1,2,3]，这里必须先拼接好
     * @param ids
     * @return
     */
    @RequestMapping(value = "/user/batch/{ids}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE)
    List<User> getUsers(@PathVariable("ids") String ids);

    @RequestMapping(value = "/user/echo/by/json", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    User echoByJson(User user);

    @RequestMapping(value = "/user/echo/by/xml", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
    User echoByXml(User user);
}
